from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.urls import reverse
from .models import UserId,UserMaterial,Encrypted

# Create your views here.

#注册页面
def register(request):
    context = {}
    return render(request,'user/register.html',context)



#注册账号运算
def zhuce_yunsuan(request):
    info = request.POST
    #info = {'user':123,'pswd':123456}
    user = info['user']
    pswd = info['pswd']
    try:
        UserId.objects.create(user_account = user,user_pswd = pswd)
    except:
        return HttpResponse('账号已存在')
    else:
        return HttpResponse('注册成功')

#登录界面
def login(request):
    context = {}
    return render(request,'user/login.html',context)



#登录运算
def login_yunsuan(request):
    info = request.POST
    user = info['user']
    pswd = info['pswd']
    try:
        user_obj = UserId.objects.get(user_account = user)
    except UserId.DoesNotExist:
        return HttpResponse('账号不存在')
    else:
        if pswd == user_obj.user_pswd:
            request.session['user'] = user
            return HttpResponseRedirect(reverse('user:user_home',args = (user_obj.user_account,)))
        else:
            return HttpResponse('密码错误')
    

#登陆后的界面
def user_home(request,user_account):
    try:
        request.session['user']
        
    except KeyError:
        return HttpResponseRedirect(reverse('CRHMweb:home'))
    else:
        context = {'user_account':user_account}
        
        return render(request,'CRHMweb/CRHMweb.html',context)




#个人中心展示的页面
def my_center(request,user_account):
    context = {'user_account':user_account}
    return render(request,'user/user_center.html',context)

'''
#编辑资料
def edit_data(request,user_account):
    if request.method == "POST":
        
    else:
        context = {'user_account':user_account}
        return render(request,'user/edit_data.html',context)

'''
































